﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.PopupDialog;
using Aliyeye.Car.Net.UI.Common;

namespace Aliyeye.Car.Net.UI.SystemModel
{
    public partial class Company : BaseForm
    {
        CompanySvc.SoapHeaderClass sopaHeader = new CompanySvc.SoapHeaderClass();
        public Company()
        {
            InitializeComponent();
        }
        public int comId = 0;
        private void btnAdd_Click(object sender, EventArgs e)
        {
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            CompanySvc.Company model = new CompanySvc.Company();
            model.ComAddr = txtAddr.Text;
            model.ComName = txtname.Text;

            model.Marks = txtmarks.Text;
            model.Tel = txttel.Text;
            model.Url = txtCode.Text;
            model.UserName = txtUser.Text;
            model.Fax = txtFax.Text;
            model.EMail = txtEMail.Text;
            model.BankName = txtBank.Text;
            model.BankAccount = txtBankAccount.Text;

            if (comId > 0)
            {
                model.Id = comId;
                ServiceManipulator.SafeAsyncExecution<CompanySvc.CompanyServiceSoapClient>(
                    proxy => proxy.Update(sopaHeader, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("公司信息修改成功！");
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
            else
            {
                ServiceManipulator.SafeAsyncExecution<CompanySvc.CompanyServiceSoapClient>(
                    proxy => proxy.Add(sopaHeader, model),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            PageCommon.Notify("公司信息添加成功！");
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
            }
        }

        private void Company_Load(object sender, EventArgs e)
        {
            DataSet ds = null;
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            ServiceManipulator.SafeAsyncExecution<CompanySvc.CompanyServiceSoapClient>(
                    proxy => proxy.GetList(sopaHeader, ""),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            ds = (DataSet)result.Result;
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                comId = int.Parse(ds.Tables[0].Rows[0][0].ToString());
                                Model.Company model = new Model.Company();

                                model.ComName = ds.Tables[0].Rows[0]["ComName"].ToString();
                                model.ComAddr = ds.Tables[0].Rows[0]["ComAddr"].ToString();
                                model.Tel = ds.Tables[0].Rows[0]["Tel"].ToString();
                                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                                model.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                                model.Fax = ds.Tables[0].Rows[0]["Fax"].ToString();
                                model.EMail = ds.Tables[0].Rows[0]["EMail"].ToString();

                                model.BankName = ds.Tables[0].Rows[0]["BankName"].ToString();
                                model.BankAccount = ds.Tables[0].Rows[0]["BankAccount"].ToString();

                                txtAddr.Text = model.ComAddr.ToString();
                                txtCode.Text = model.Url.ToString();
                                txtmarks.Text = model.Marks.ToString();

                                txtname.Text = model.ComName.ToString();
                                txttel.Text = model.Tel.ToString();
                                txtUser.Text = model.UserName.ToString();
                                txtFax.Text = model.Fax;
                                txtEMail.Text = model.EMail;
                                txtBank.Text = model.BankName;
                                txtBankAccount.Text = model.BankAccount;
                            }
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
        }

        private void menuBlueButton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
